//package com.gradle.quartz.config;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.gradle.quartz.bean.User;
//import org.springframework.batch.core.Job;
//import org.springframework.batch.core.Step;
//import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
//import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
//import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
//import org.springframework.batch.core.launch.support.RunIdIncrementer;
//import org.springframework.batch.core.step.tasklet.Tasklet;
//import org.springframework.batch.item.json.JacksonJsonObjectMarshaller;
//import org.springframework.batch.item.json.JacksonJsonObjectReader;
//import org.springframework.batch.item.json.JsonFileItemWriter;
//import org.springframework.batch.item.json.JsonItemReader;
//import org.springframework.batch.item.json.builder.JsonFileItemWriterBuilder;
//import org.springframework.batch.item.json.builder.JsonItemReaderBuilder;
//import org.springframework.batch.repeat.RepeatStatus;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.io.InputStreamResource;
//import org.springframework.core.io.PathResource;
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.FileNotFoundException;
//
///**
// * 逻辑现有两条
// * 第一条普通任务 job ---> step ---> tasklet
// * 第二条：要读取文件
// * job ---> step ---> chunk ---> reader ----> writer
// */
//@EnableBatchProcessing
//@Configuration
//public class JsonBatchConfig {
//    @Autowired
//    private JobBuilderFactory jobBuilderFactory;
//    @Autowired
//    private StepBuilderFactory stepBuilderFactory;
//    @Bean
//    public JacksonJsonObjectMarshaller<User> objectMarshaller(){
//        return new JacksonJsonObjectMarshaller<>();
//    }
//
//
//    @Bean
//    public JsonFileItemWriter<User> jsonFileItemWriter() {
//        return new JsonFileItemWriterBuilder<User>()
//                .name("userJsonItemWriter")
//                .resource(new PathResource("D:\\workspace\\mygradleproject\\readerOut\\readerOut.json"))
//                .jsonObjectMarshaller(objectMarshaller())
//                .build();
//    }
//    @Bean
//    public JsonItemReader<User> itemReader() throws FileNotFoundException {
//        ObjectMapper objectMapper = new ObjectMapper();
//        JacksonJsonObjectReader<User> userReader = new JacksonJsonObjectReader<>(User.class);
//        userReader.setMapper(objectMapper);
//        File file = new File("D:\\workspace\\mygradleproject\\gradle-quartz\\src\\main\\resources\\file\\user.json");
//        return new JsonItemReaderBuilder<User>()
//                .name("userJsonItemReader")
//                .resource(new InputStreamResource(new FileInputStream(file)))
//                .jsonObjectReader(userReader)
//                .build();
//    }
//    /**
//     * 处理任务的逻辑对象
//     *
//     * @return Tasklett
//     */
//    @Bean
//    public Tasklet taskLet() {
//        return (contribution, chunkContext) -> {
//            System.out.println(">>>>>>>>>jsonTaskLet->处理任务的逻辑对象【jsonTaskLet】>>>>>>>>");
//            return RepeatStatus.FINISHED;
//        };
//    }
//
//    @Bean("jsonStep")
//    public Step step() throws FileNotFoundException {
//        System.out.println(">>>>>>>>>jsonStep-->【jsonStep】>>>>>>>>");
//        return stepBuilderFactory.get("step1")
//                //一次性读多少数据,以及数据类型为User
//                .<User,User>chunk(1)
//                .reader(itemReader())
//                .writer(jsonFileItemWriter())
//                .build();
//    }
//
//    @Bean("jsonJob")
//    public Job job() throws Exception {
//        System.out.println(">>>>>>>>>jsonJob--> 任务启动执行对象【jsonJob】>>>>>>>>");
//        return jobBuilderFactory.get("json-processor-job")
//                .start(step())
//                .incrementer(new RunIdIncrementer())
//                .build();
//    }
//}
